import 'package:flutter/material.dart';
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart';
import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart';
import 'package:flutter_smart_heating/common/app.dart';
import 'package:flutter_smart_heating/common/http.dart';
import 'package:flutter_smart_heating/pages/basic/company/model.dart';
import 'package:flutter_smart_heating/pages/information/map/map.dart';
import 'package:flutter_smart_heating/ui/ui.dart';
import 'package:get/get.dart';

class BasicCompanyDetailPage extends StatefulWidget {
  const BasicCompanyDetailPage({super.key});

  @override
  State<BasicCompanyDetailPage> createState() => _BasicCompanyDetailPageState();
}

class _BasicCompanyDetailPageState extends State<BasicCompanyDetailPage> {
  dynamic id;
  BasicCompanyModel company = BasicCompanyModel();
  @override
  void initState() {
    id = Get.parameters['id'];
    super.initState();
  }

  fetchData() async {
    var res =
        await Http.instance.post('/api/basic/company/detail', data: {'id': id});

    setState(() {
      company = BasicCompanyModel.fromJson(res.data);
    });
  }

  bool get isBlock => company.id == null;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(company.bname ?? '企业详情'),
        actions: const [
          UiRightMenuButtonGroup(),
        ],
      ),
      body: UiLifeCycle(
        onRouteDone: fetchData,
        child: Column(
          children: [
            Expanded(
              child: ListView(
                padding: const EdgeInsets.only(
                  top: UiTheme.toolbarSpacing,
                  left: UiTheme.spacingX,
                  right: UiTheme.spacingX,
                ),
                children: [
                  UiPanel(
                    padding: const EdgeInsets.all(UiTheme.spacingX),
                    children: [
                      ClipRRect(
                        borderRadius: BorderRadius.circular(UiTheme.boxRadius),
                        child: AspectRatio(
                          aspectRatio: 16 / 9,
                          child: company.image == '' || company.image == null
                              ? Container(
                                  color: Colors.grey.withOpacity(.2),
                                )
                              : UiImage(
                                  imageUrl: company.image ?? '',
                                  fit: BoxFit.cover,
                                  zoomable: true,
                                ),
                        ),
                      ),
                      const SizedBox(height: UiTheme.spacingX),
                      isBlock
                          ? UiBlock(height: 20, width: Get.size.width - 100)
                          : Text(
                              company.name ?? '智慧供热',
                              style: const TextStyle(
                                fontSize: 18,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                      const SizedBox(height: 5),
                      isBlock
                          ? Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: List.generate(
                                3,
                                (index) => UiBlock(
                                  height: 13.3,
                                  width:
                                      Get.size.width - 100 - (index + 1) * 40,
                                  margin: 5,
                                ),
                              ),
                            )
                          : UiFoldUpText(
                              text: company.intro ?? '--',
                              textStyle: TextStyle(
                                height: 1.6,
                                color: UiTheme(context).match(
                                  light: Colors.black,
                                ),
                              ),
                              spanTextStyle:
                                  const TextStyle(color: UiTheme.primary),
                              maxLines: 2,
                            )
                    ],
                  ),
                  const UiLineBreak(),
                  UiPanel(
                    children: [
                      cell(
                        icon: UiIcons.station,
                        title: '查看换热站',
                        trailing: Text('${company.stationTotal ?? 0}'),
                        showDivider: false,
                        showArrow: true,
                        onTap: () {
                          App.push(
                              '/basic/station?company_id=${company.id}&company_name=${company.bname}');
                        },
                      ),
                    ],
                  ),
                  const UiLineBreak(),
                  UiPanel(
                    children: [
                      cell(
                        icon: UiIcons.solution,
                        title: '负责人',
                        trailing:
                            Text(Ui.getString(company.leaderLinkman, '--')),
                      ),
                      cell(
                        icon: UiIcons.mobile,
                        title: '负责人电话',
                        trailing:
                            Text(Ui.getString(company.leaderLinkphone, '--')),
                        onTap: () {
                          Ui.makePhoneCall(company.leaderLinkphone);
                        },
                      ),
                      cell(
                        icon: UiIcons.user,
                        title: '技术员',
                        trailing: Text(Ui.getString(company.techLinkman, '--')),
                      ),
                      cell(
                        icon: UiIcons.mobile,
                        title: '技术员电话',
                        trailing:
                            Text(Ui.getString(company.techLinkphone, '--')),
                        onTap: () {
                          Ui.makePhoneCall(company.techLinkphone);
                        },
                      ),
                      cell(
                        icon: UiIcons.phone,
                        title: '客服电话',
                        trailing: Text(Ui.getString(company.serviceTel, '--')),
                        showDivider: false,
                        onTap: () {
                          Ui.makePhoneCall(company.serviceTel);
                        },
                      ),
                    ],
                  ),
                  const UiLineBreak(),
                  UiPanel(
                    children: [
                      cell(
                        icon: UiIcons.location,
                        title: Ui.getString(company.address, '暂无地址'),
                        showDivider: false,
                        showArrow: true,
                        onTap: () {
                          InformationMapPage.show(context,
                              name: company.bname,
                              address: company.address,
                              location: MapCoordinate(
                                double.tryParse(company.latitude ?? '0') ?? 0,
                                double.tryParse(company.longitude ?? '0') ?? 0,
                              ));
                          //Ui.copyText(company.address);
                        },
                      ),
                      if (company.showMap == true)
                        SizedBox(
                          height: 250,
                          child: BMFMapWidget(
                            onBMFMapCreated: (controller) {
                              BMFText bmfText = BMFText(
                                text: '🏠 ${company.bname} ',
                                paragraphSpacing: 10,
                                position: BMFCoordinate(
                                  double.tryParse(company.latitude ?? '0') ?? 0,
                                  double.tryParse(company.longitude ?? '0') ??
                                      0,
                                ),
                                bgColor: UiTheme.primary,
                                fontColor: Colors.white,
                                fontSize: 15,
                                alignment: BMFTextAlignment.center,
                                typeFace: BMFTypeFace(
                                  textStype: BMFTextStyle.BOLD,
                                ),
                                zIndex: 100,
                                lineSpacing: 10,
                                rotate: 0,
                              );

                              controller.addText(bmfText);
                            },
                            mapOptions: BMFMapOptions(
                              overlookEnabled: false,
                              rotateEnabled: false,
                              center: BMFCoordinate(
                                double.tryParse(company.latitude ?? '0') ?? 0,
                                double.tryParse(company.longitude ?? '0') ?? 0,
                              ),
                              zoomLevel: 16,
                            ),
                          ),
                        ),
                    ],
                  ),
                ],
              ),
            ),
            SafeArea(
              top: false,
              bottom: true,
              minimum: const EdgeInsets.only(bottom: 20),
              child: Container(),
            ),
          ],
        ),
      ),
    );
  }

  Widget cell({
    required IconData icon,
    required String title,
    Function()? onTap,
    bool showDivider = true,
    bool showArrow = false,
    Widget? trailing,
  }) {
    return UiListCell(
      onTap: onTap,
      leading: UiLinearGradientMask(
          child: UiIcon(
        icon,
        color: Colors.white,
      )),
      leadingSpace: 10,
      title: isBlock ? const UiBlock(height: 15, width: 300) : Text(title),
      showArrow: isBlock ? false : showArrow,
      showDivider: showDivider,
      dividerIndent: 50,
      trailing: isBlock ? null : trailing,
      backgroundColor: Colors.transparent,
    );
  }
}
